/**
 * @author Pieter Michels / wellconsidered
 * pieter@wellconsidered.be
 *
 * Open source under the GNU Lesser General Public License (http://www.opensource.org/licenses/lgpl-license.php)
 * Copyright © 2009 Pieter Michels / wellconsidered
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this library; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
 
package be.wellconsidered.apis.lighthouse.resources
{
	import be.wellconsidered.apis.lighthouse.LightHouse;
	import be.wellconsidered.apis.lighthouse.connectors.*;
	import be.wellconsidered.apis.lighthouse.connectors.events.*;
	import be.wellconsidered.apis.lighthouse.resources.events.MilestoneEvents;
	import be.wellconsidered.logging.Logger;
	
	public class Milestones extends AbstractResource
	{		
		private var _xmlNewMilestone:XML;
		
		public function Milestones(lighthouseReference:LightHouse)
		{
			super(lighthouseReference);
		}
		
		/**
		 * List Milestones
		 */
		public function listMilestones(projectID:int):void
		{
			Logger.log("List Milestones");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/milestones.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onMilestoneListingComplete, false, 0, true);
			
			function onMilestoneListingComplete(e:GETEvents):void
			{
				dispatchEvent(new MilestoneEvents(MilestoneEvents.LIST_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Get Milestone
		 */
		public function getMilestone(projectID:int, milestoneID:int):void
		{
			Logger.log("Get Milestone (" + milestoneID + ")");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/milestones/" + milestoneID + ".xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onMilestoneFetchComplete, false, 0, true);
			
			function onMilestoneFetchComplete(e:GETEvents):void
			{
				dispatchEvent(new MilestoneEvents(MilestoneEvents.GET_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * New Milestone
		 */
		public function newMilestone(projectID:int):void
		{
			Logger.log("New Milestone");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/milestones/new.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onNewMilestoneDataComplete, false, 0, true);
			
			function onNewMilestoneDataComplete(e:GETEvents):void
			{
				_xmlNewMilestone = new XML(e.data);
				
				dispatchEvent(new MilestoneEvents(MilestoneEvents.NEW_COMPLETE, false, false, _xmlNewMilestone));	
			}
		}
		
		/**
		 * Create Milestone
		 */
		public function createMilestone(projectID:Number, milestoneTitle:String, milestoneBody:String, milestoneState:String, milestoneAssignedUserID:int = -1, milestoneMilestoneID:int = -1, milestoneArrayOfTags:Array = null):void
		{
			Logger.log("Create Milestone");
			
			var xmlMilestone:XML = _xmlNewMilestone;
			
			xmlMilestone..title = milestoneTitle;
			xmlMilestone..body = milestoneBody;
			xmlMilestone..state = milestoneState;
			
			if(milestoneAssignedUserID > 0)
				xmlMilestone..elements("assigned-user-id").nodeValue = milestoneAssignedUserID;
			
			if(milestoneMilestoneID > 0)
				xmlMilestone..elements("milestone-id").nodeValue = milestoneMilestoneID;;
			
			if(milestoneArrayOfTags != null)
				xmlMilestone..tag = milestoneArrayOfTags.toString();
			
			trace(xmlMilestone);
					
			var connPOST:POST = new POST(_lhRef.getURL() + "projects/" + projectID + "/milestones.xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), xmlMilestone);
			
			connPOST.addEventListener(POSTEvents.COMPLETE, onCreateMilestoneComplete, false, 0, true);
			
			function onCreateMilestoneComplete(e:POSTEvents):void
			{				
				dispatchEvent(new MilestoneEvents(MilestoneEvents.CREATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Update Milestone
		 */
		public function updateMilestone(projectID:int, milestoneXML:XML):void
		{
			Logger.log("Update Milestone (" + milestoneXML.number + ")");
			
			var connPUT:PUT = new PUT(_lhRef.getURL() + "projects/" + projectID + "/milestones/" + milestoneXML.id + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), milestoneXML);
			
			connPUT.addEventListener(PUTEvents.COMPLETE, onUpdateMilestoneComplete, false, 0, true);
			
			function onUpdateMilestoneComplete(e:PUTEvents):void
			{				
				dispatchEvent(new MilestoneEvents(MilestoneEvents.UPDATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Delete Milestone
		 */
		public function deleteMilestone(projectID:int, milestoneID:int):void
		{
			Logger.log("Delete Milestone (" + milestoneID + ")");
			
			var connDELETE:DELETE = new DELETE(_lhRef.getURL() + "projects/" + projectID + "/milestones/" + milestoneID + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding());
			
			connDELETE.addEventListener(DELETEEvents.COMPLETE, onDeleteMilestoneComplete, false, 0, true);
			
			function onDeleteMilestoneComplete(e:DELETEEvents):void
			{				
				dispatchEvent(new MilestoneEvents(MilestoneEvents.DELETE_COMPLETE, false, false, e.data));	
			}
		}
	}
}